package ru.usu.gv.gui.routing;

import com.sun.j3d.utils.geometry.Sphere;

import ru.usu.gv.core.GraphCanvas;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.LineStripArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import java.awt.Color;

/**
 * @author spupyrev
 * 28.10.2009
 */
public class DebugUtilities
{
	public static void showLine(Color color, Point3d p1, Point3d p2)
	{
		Point3d[] coordinates = new Point3d[2];
		coordinates[0] = p1;
		coordinates[1] = p2;
		showPolyline(color, coordinates);
	}

	public static void showPolyline(Color color, Point3d[] coordinates)
	{
		BranchGroup branchGroup = new BranchGroup();
		Shape3D shape = new Shape3D();
		shape.setAppearance(new Appearance());
		shape.getAppearance().setColoringAttributes(new ColoringAttributes());
		shape.getAppearance().getColoringAttributes().setColor(new Color3f(color));
		branchGroup.addChild(shape);

		LineStripArray lineStripArray = new LineStripArray(coordinates.length, GeometryArray.COORDINATES, new int[] { coordinates.length });
		lineStripArray.setCoordinates(0, coordinates);

		shape.setGeometry(lineStripArray);

		GraphCanvas.getInstance().addChild(branchGroup);
	}

	public static void showCircle(Color color, Point3d center, double radius)
	{
		BranchGroup branchGroup = new BranchGroup();
		Sphere sphere = new Sphere((float) radius);
		sphere.setAppearance(new Appearance());
		sphere.getAppearance().setColoringAttributes(new ColoringAttributes());
		sphere.getAppearance().getColoringAttributes().setColor(new Color3f(color));
		TransformGroup tg = new TransformGroup();
		tg.addChild(sphere);
		Transform3D transform = new Transform3D();
		transform.setTranslation(new Vector3d(center));
		tg.setTransform(transform);

		BranchGroup subBranchGroup = new BranchGroup();
		subBranchGroup.setCapability(BranchGroup.ALLOW_DETACH);
		subBranchGroup.addChild(tg);
		branchGroup.addChild(subBranchGroup);

		GraphCanvas.getInstance().addChild(branchGroup);
	}
}
